package com.big.map.controller;

import com.big.common.result.Result;
import com.big.common.utils.AddressUtil;
import com.big.map.service.MapService;
import com.big.model.model.map.CitiesVo;
import com.big.model.vo.map.MapVo;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@RestController
@RequestMapping("/admin/map")
public class MapController {

    @Autowired
    MapService mapService;

    //创建读写锁对象
    private ReadWriteLock rwLock = new ReentrantReadWriteLock();

    /**
     * 坐标图
     * @return
     */
    @ApiOperation(value = "坐标图")
    @GetMapping("/coordinates/{oId}")
    public Result coordinateInfosList(@PathVariable("oId") String oId) throws IOException {
        System.out.println(oId);
        //上读锁
        rwLock.readLock().lock();
        List<double[]> stringList = mapService.coordinateInfosList(oId);
        //解锁读锁
        rwLock.readLock().unlock();
        return Result.ok(stringList);
    }

    /**
     * 坐标图
     * @return
     */
    @ApiOperation(value = "坐标图")
    @GetMapping("/toponymList/{oId}")
    public Result toponymList(@PathVariable("oId") String oId) throws IOException {
        System.out.println(oId);
        List<CitiesVo> citiesVoList = mapService.toponymList(oId);
        return Result.ok(citiesVoList);
    }


    @ApiOperation(value = "浏览器IP定位")
    @PostMapping("/ip")
    public Result ipUrl(MapVo mapVo) throws IOException {
        System.out.println("ip"+mapVo.getIp());
        String[] split = mapVo.getIp().split(",");
        String s = mapService.latitudeAndLongitudeAddress(split[1], split[0]);
        System.out.println(s);
        Map<String, String> addressInfo = AddressUtil.getAddressInfo(s);
        return Result.ok(addressInfo);
    }

}
